import { cloudDBZone, initCloudDB } from '@/AGC/manager'
import { CloudDBZoneQuery } from '@hw-agconnect/database'
import { FoodRecord } from '@/AGC/model/FoodRecord'
import { ExerciseRecord } from '@/AGC/model/ExerciseRecord'
import { useUserStore } from '@/stores/user'
import { WeightRecord } from '@/AGC/model/WeightRecord'

/**
 * 查询用户食物记录
 * @returns 食物记录
 */
export const queryUserFoodRecord = async () => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(FoodRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id).orderByAsc('id')
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 查询用户运动记录
 * @returns 运动记录
 */
export const queryUserExerciseRecord = async () => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(ExerciseRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id).orderByAsc('id')
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 执行用户体重记录插入
 * @param record 体重记录
 */
export const executeWeightRecord = async (record: WeightRecord) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeUpsert(record)
  console.log('executeWeightRecord: ', cloudDBResult)
}

/**
 * 根据月份查询用户的体重记录
 * @param month 月份
 * @returns 体重记录
 */
export const queryWeightRecordByDate = async (month: string) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(WeightRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id).contains('date', month).orderByAsc('id')
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 执行用户食物记录删除
 * @param record 食物记录
 */
export const executeDeleteFoodRecord = async (record: FoodRecord) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeDelete(record)
  console.log('executeDeleteFoodRecord: ', cloudDBResult)
}

/**
 * 执行用户食物记录插入
 * @param foodRecord 食物记录
 */
export const executeFoodRecord = async (foodRecord: FoodRecord) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeUpsert(foodRecord)
  console.log('executeFoodRecord: ', cloudDBResult)
}

/**
 * 根据日期查询用户的食物记录
 * @param date 日期
 * @returns 食物记录
 */
export const queryFoodRecordByDate = async (date: string) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(FoodRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id).equalTo('date', date).orderByAsc('id')
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 执行用户运动记录删除
 * @param record 运动记录
 */
export const executeDeleteExerciseRecord = async (record: ExerciseRecord) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeDelete(record)
  console.log('executeDeleteExerciseRecord: ', cloudDBResult)
}

/**
 * 执行用户运动记录插入
 * @param recordList 运动记录
 */
export const executeExerciseRecord = async (record: ExerciseRecord) => {
  if (!cloudDBZone) await initCloudDB()

  const cloudDBResult = await cloudDBZone.executeUpsert(record)
  console.log('executeExerciseRecord: ', cloudDBResult)
}

/**
 * 根据日期查询用户的运动记录
 * @param date 日期
 * @returns 运动记录
 */
export const queryExerciseRecordByDate = async (date: string) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(ExerciseRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id).equalTo('date', date).orderByAsc('id')
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 根据日期列表查询用户的运动记录
 * @param dateList 日期列表
 * @returns 运动记录
 */
export const queryExerciseRecordByDateList = async (dateList: string[]) => {
  if (!cloudDBZone) await initCloudDB()

  const query = CloudDBZoneQuery.where(ExerciseRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id).in('date', dateList).orderByAsc('id')
  const snapshot = await cloudDBZone.executeQuery(query)
  return snapshot.getSnapshotObjects()
}

/**
 * 清除用户的所有食物记录
 */
export const executeDeleteAllFoodRecord = async () => {
  if (!cloudDBZone) await initCloudDB()

  const query = await CloudDBZoneQuery.where(FoodRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id)
  const snapshot = await cloudDBZone.executeQuery(query)
  const list = snapshot.getSnapshotObjects()
  await cloudDBZone.executeDelete(list)
}

/**
 * 清除用户的所有运动记录
 */
export const executeDeleteAllExerciseRecord = async () => {
  if (!cloudDBZone) await initCloudDB()

  const query = await CloudDBZoneQuery.where(ExerciseRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id)
  const snapshot = await cloudDBZone.executeQuery(query)
  const list = snapshot.getSnapshotObjects()
  await cloudDBZone.executeDelete(list)
}

/**
 * 清除用户的所有体重记录
 */
export const executeDeleteAllWeightRecord = async () => {
  if (!cloudDBZone) await initCloudDB()

  const query = await CloudDBZoneQuery.where(WeightRecord)
  const user_id = useUserStore().userInfo.id
  query.equalTo('user_id', user_id)
  const snapshot = await cloudDBZone.executeQuery(query)
  const list = snapshot.getSnapshotObjects()
  await cloudDBZone.executeDelete(list)
}
